package com.ruoyi.material.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.base.util.GenCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.material.domain.CategoryInfo;
import com.ruoyi.material.service.ICategoryInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 类别信息Controller
 * 
 * @author ruoyi
 * @date 2022-08-23
 */
@Api("类别管理")
@RestController
@RequestMapping("/material/material")
public class CategoryInfoController extends BaseController
{
    @Autowired
    private ICategoryInfoService categoryInfoService;

    /**
     * 查询类别信息列表
     */
    @ApiOperation("类别列表查询")
    @PreAuthorize("@ss.hasPermi('material:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(CategoryInfo categoryInfo)
    {
        startPage();
        List<CategoryInfo> list = categoryInfoService.selectCategoryInfoList(categoryInfo);
        return getDataTable(list);
    }

    /**
     * 导出类别信息列表
     */
    @PreAuthorize("@ss.hasPermi('material:material:export')")
    @Log(title = "类别信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CategoryInfo categoryInfo)
    {
        List<CategoryInfo> list = categoryInfoService.selectCategoryInfoList(categoryInfo);
        ExcelUtil<CategoryInfo> util = new ExcelUtil<CategoryInfo>(CategoryInfo.class);
        util.exportExcel(response, list, "类别信息数据");
    }

    /**
     * 获取类别信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('material:material:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(categoryInfoService.selectCategoryInfoById(id));
    }

    /**
     * 新增类别信息
     */
    @PreAuthorize("@ss.hasPermi('material:material:add')")
    @Log(title = "类别信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CategoryInfo categoryInfo)
    {
        categoryInfo.setCategoryNo(GenCodeUtils.getMaterialCategoryNo());
        return toAjax(categoryInfoService.insertCategoryInfo(categoryInfo));
    }

    /**
     * 修改类别信息
     */
    @PreAuthorize("@ss.hasPermi('material:material:edit')")
    @Log(title = "类别信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CategoryInfo categoryInfo)
    {
        return toAjax(categoryInfoService.updateCategoryInfo(categoryInfo));
    }

    /**
     * 删除类别信息
     */
    @PreAuthorize("@ss.hasPermi('material:material:remove')")
    @Log(title = "类别信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(categoryInfoService.deleteCategoryInfoByIds(ids));
    }
}
